﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Collections;

namespace WcfCore
{
	public static class WcfClientFactory
	{
		public static IWcfServiceContract CreateWcfClient(WcfTestInput input)
		{
			WcfLogger.Log(string.Format("Creating proxy in AppDomain Id: {0}.", AppDomain.CurrentDomain.Id));

			object obj;
			if (input.Scope.Equals(PerformanceTestScope.Direct))
			{
				obj = Activator.CreateInstance(input.ServiceType);
			}
			else if (input.Binding.Equals(Bindings.Remoting))
			{
				if (input.Scope.Equals(PerformanceTestScope.SameMachine))
				{
					Hashtable properties = new Hashtable();
					properties.Add("port", 0);
					BinaryServerFormatterSinkProvider srvSinkProv = new BinaryServerFormatterSinkProvider();
					srvSinkProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
					BinaryClientFormatterSinkProvider cliSinkProv = new BinaryClientFormatterSinkProvider();
					TcpChannel channel = new TcpChannel(properties, cliSinkProv, srvSinkProv);
					ChannelServices.RegisterChannel(channel, false);
				}
				obj = Activator.GetObject(input.ServiceType, input.RemotingUrl);
			}
			else
			{
				obj = Activator.CreateInstance(input.ServiceProxyType);
			}

			return obj as IWcfServiceContract;
		}
	}
}
